package cn.zptc.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.zptc.entity.Employee;
import cn.zptc.utils.C3p0Util;

public class EmployeeDAO {

		// 添加员工方法
		public int insertEmployee(Employee employee) throws SQLException {
			String sql= "INSERT INTO `ems`.`employee` (`name`,`phone`,`department_id`) VALUES  (?,?,?)";
			QueryRunner runner= new QueryRunner(C3p0Util.getDataSource());
			int row =runner.update(sql, employee.getName(),employee.getPhone(),employee.getDepartmentId());
			return row;
		}
	 
		// 获取员工列表
		public List<Employee> selectAllEmployees() throws SQLException {
			String sql="SELECT e.`id`,e.`name`,e.`phone`, d.`name` AS departmentName " + 
					"FROM `employee` e LEFT JOIN `department` d ON e.`department_id` = d.`id`";
			QueryRunner runner=new QueryRunner(C3p0Util.getDataSource());
			List<Employee> list = runner.query(sql, new BeanListHandler<Employee>(Employee.class));		
			return list;		
		}

}
